package com.uchengwang.ui.cm_ui.ucity.ui;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.uchengwang.ui.base_ui.ui.AllBaseActivity;
import com.uchengwang.ui.listener.PermissionsListener;
import com.uchengwang.ui.utils.PermissionUtils;
import com.uchengwang.ui.utils.StatusBarUtil;
import com.uchengwang.ui.utils.UCommonUtil;
import com.uchengwang.ui.view.EasePhotoView;
import com.uchengwang.ui.view.MyViewPager;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by yiw on 2016/1/6.
 */
public class ImagePagerActivity extends AllBaseActivity{

    private List<View> guideViewList = new ArrayList<View>();
    private LinearLayout guideGroup;
    private ActivityTitleBar titleBar;
    private MyViewPager viewPager;
    private ArrayList<String> imgUrls;
    private TextView position;
    private String currentImage;//当前图片地址
    private int pubType = 0;//1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
    private boolean isLocal = true;//是不是本地图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_imagepager);
        StatusBarUtil.getUtil().setStatusBarColor(this, R.color.sign_color);

        initView();

    }

    private void initView(){
        titleBar = (ActivityTitleBar) findViewById(R.id.images_titleBar);
        titleBar.setTitle("图片", R.color.white);
        titleBar.getRlLeft().setBackgroundResource(R.color.sign_color);
        titleBar.setTitleBarBackground(R.color.sign_color);
        titleBar.setLeftView("", 0, R.drawable.icon_back_white, null);

//        titleBar.setTitleBarBackground(R.color.sign_color);


        viewPager = (MyViewPager) findViewById(R.id.pager);
        position = (TextView) findViewById(R.id.position);
        guideGroup = (LinearLayout) findViewById(R.id.guideGroup);
        int startPos = getIntent().getIntExtra(BaseConfig.INTENT_POSITION, 0);
        imgUrls = getIntent().getStringArrayListExtra(BaseConfig.INTENT_IMGURLS);
        pubType = getIntent().getIntExtra(BaseConfig.PUB_TYPE, 0);
        isLocal = getIntent().getBooleanExtra(BaseConfig.IS_LOCAL, true);

        if (!isLocal){
            titleBar.getRightTV().setTextColor(getResources().getColor(R.color.white));
            titleBar.setRightText("保存", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    PermissionUtils.getUtils().checkStorage(ImagePagerActivity.this, new PermissionsListener() {
                        @Override
                        public void onSuccess() {
                            //下载图片
                            OSSManager.getOSSManager().downImageAsync(ImagePagerActivity.this, pubType, currentImage);
                        }

                        @Override
                        public void onFailed() {
                            UCommonUtil.getUtil().showToast(ImagePagerActivity.this, "授权取消");
                        }
                    });



                }
            });
        }


        ImageAdapter mAdapter = new ImageAdapter(this);
        mAdapter.setDatas(imgUrls);
        viewPager.setAdapter(mAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ImagePagerActivity.this.position.setText(String.format("%d/%d", position + 1, imgUrls.size()));
                currentImage = imgUrls.get(position);
                for (int i = 0; i < guideViewList.size(); i++) {
                    guideViewList.get(i).setSelected(i == position ? true : false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(startPos);
        if (startPos ==0){
            position.setText(String.format("%d/%d", 1, imgUrls.size()));
        }
        currentImage = imgUrls.get(startPos);

        if (imgUrls != null && imgUrls.size() >1){
            addGuideView(guideGroup, startPos, imgUrls);
        }

    }


    private void addGuideView(LinearLayout guideGroup, int startPos, ArrayList<String> imgUrls) {
        if (imgUrls != null && imgUrls.size() > 0) {
            guideViewList.clear();
            for (int i = 0; i < imgUrls.size(); i++) {
                View view = new View(this);
                view.setBackgroundResource(R.drawable.selector_guide_bg);
                view.setSelected(i == startPos ? true : false);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.gudieview_width),
                        getResources().getDimensionPixelSize(R.dimen.gudieview_heigh));
                layoutParams.setMargins(10, 0, 0, 0);
                guideGroup.addView(view, layoutParams);
                guideViewList.add(view);
            }
        }
    }

    private class ImageAdapter extends PagerAdapter {

        private List<String> datas = new ArrayList<String>();
        private LayoutInflater inflater;
        private Context context;

        public void setDatas(List<String> datas) {
            if (datas != null)
                this.datas = datas;
        }

        public ImageAdapter(Context context) {
            this.context = context;
            this.inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            if (datas == null) return 0;
            return datas.size();
        }


        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            View view = inflater.inflate(R.layout.item_pager_image, container, false);
            if (view != null) {
                final EasePhotoView imageView = (EasePhotoView) view.findViewById(R.id.image);
                //预览imageView
                final ImageView smallImageView = new ImageView(context);
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                layoutParams.gravity = Gravity.CENTER;
                smallImageView.setLayoutParams(layoutParams);
                smallImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                ((FrameLayout) view).addView(smallImageView);

                final String imgurl = datas.get(position);
                ImageManager.getInstance().loadImage(context, imageView, imgurl, ImageManager.ImageType.NARMOL);


                container.addView(view, 0);
            }
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }


    }
}
